package cn.iocoder.yudao.module.handguard.controller.app.processrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.app.processrecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.processrecord.ProcessRecordDO;
import cn.iocoder.yudao.module.handguard.convert.processrecord.ProcessRecordConvert;
import cn.iocoder.yudao.module.handguard.service.processrecord.ProcessRecordService;

@Tag(name = "管理后台 - 用户申请贷款流程处理记录")
@RestController
@RequestMapping("/hutai/process-record")
@Validated
public class ProcessRecordController {

    @Resource
    private ProcessRecordService processRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建用户申请贷款流程处理记录")
    public CommonResult<Long> createProcessRecord(@Valid @RequestBody ProcessRecordCreateReqVO createReqVO) {
        return success(processRecordService.createProcessRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户申请贷款流程处理记录")
    public CommonResult<Boolean> updateProcessRecord(@Valid @RequestBody ProcessRecordUpdateReqVO updateReqVO) {
        processRecordService.updateProcessRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户申请贷款流程处理记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteProcessRecord(@RequestParam("id") Long id) {
        processRecordService.deleteProcessRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户申请贷款流程处理记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ProcessRecordRespVO> getProcessRecord(@RequestParam("id") Long id) {
        ProcessRecordDO processRecord = processRecordService.getProcessRecord(id);
        return success(ProcessRecordConvert.INSTANCE.convert(processRecord));
    }

//    @GetMapping("/list")
//    @Operation(summary = "获得用户申请贷款流程处理记录列表")
//    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
//    public CommonResult<List<ProcessRecordRespVO>> getProcessRecordList(@RequestParam("ids") Collection<Long> ids) {
//        List<ProcessRecordDO> list = processRecordService.getProcessRecordList(ids);
//        return success(ProcessRecordConvert.INSTANCE.convertList(list));
//    }

    @GetMapping("/page")
    @Operation(summary = "获得用户申请贷款流程处理记录分页")
    public CommonResult<PageResult<ProcessRecordRespVO>> getProcessRecordPage(@Valid ProcessRecordPageReqVO pageVO) {
        PageResult<ProcessRecordDO> pageResult = processRecordService.getProcessRecordPage(pageVO);
        return success(ProcessRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户申请贷款流程处理记录 Excel")
    @OperateLog(type = EXPORT)
    public void exportProcessRecordExcel(@Valid ProcessRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProcessRecordDO> list = processRecordService.getProcessRecordList(exportReqVO);
        // 导出 Excel
        List<ProcessRecordExcelVO> datas = ProcessRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户申请贷款流程处理记录.xls", "数据", ProcessRecordExcelVO.class, datas);
    }

}
